package com.booter.producer.controller;

import com.booter.dto.JsonResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public JsonResultVo indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException e) {
        return JsonResultVo.error("500", e.getMessage());
    }




    @ExceptionHandler({HttpMessageNotReadableException.class})
    @ResponseBody
    public JsonResultVo requestNotReadable(HttpMessageNotReadableException e) {
        log.error("请求数据无法读取", e);
        return JsonResultVo.error("400", "请求数据无法读取");
    }

    /**
     * 400错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler({TypeMismatchException.class})
    @ResponseBody
    public JsonResultVo requestTypeMismatch(TypeMismatchException e) {
        log.error("参数错误", e);
        return JsonResultVo.error("400", e.getMessage());
    }


    @ExceptionHandler({MissingServletRequestPartException.class, IllegalArgumentException.class})
    @ResponseBody
    public JsonResultVo fileNotPresent(MissingServletRequestPartException e) {
        log.error("参数错误", e);
        return JsonResultVo.error("400", e.getMessage());
    }


    /**
     * 400错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseBody
    public JsonResultVo requestMissingServletRequest(MissingServletRequestParameterException e) {
        log.error("参数错误", e);
        return JsonResultVo.error("400", e.getMessage());
    }

    /**
     * 405错误
     *
     * @return
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseBody
    public JsonResultVo request405() {
        return JsonResultVo.error("405", HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase());
    }

    /**
     * 406错误
     *
     * @return
     */
    @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
    @ResponseBody
    public JsonResultVo request406() {
        return JsonResultVo.error("405", HttpStatus.NOT_ACCEPTABLE.getReasonPhrase());
    }

    /**
     * 500错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
    @ResponseBody
    public JsonResultVo server500(RuntimeException e) {
        log.error("系统内部错误", e);
        return JsonResultVo.error("500", "系统内部错误");
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResultVo handleException(Exception e) {
        log.error("系统内部错误", e);
        return JsonResultVo.error("500", "系统内部错误");
    }

}